//
//  HomeTableViewController.m
//  MobileGameLiving2
//
//  Created by reflection on 15/8/31.
//  Copyright (c) 2015年 李旭东. All rights reserved.
//

#import "HomeTableViewController.h"
#import "HomeTableViewCell.h"
#import "SearchTableViewController.h"
#import "GameLivingViewController.h"
#import "GameListTableViewController.h"
#import "GameManager.h"
#import "WelcomeView.h"
#import "leftItemView.h"

@interface HomeTableViewController ()<UIScrollViewDelegate,WelcomeViewDelegate,PageChangeDelegate>
@property (nonatomic,weak)UIScrollView *mainScrollView;
@property (nonatomic,weak)UIPageControl *mainPage;
@property (nonatomic,strong)NSArray *allWelcomeImage;
@property (nonatomic,strong)WelcomeView *welcomeView;
@property (nonatomic,strong)leftItemView *itemView;

@end

@implementation HomeTableViewController

-(NSArray *)allWelcomeImage{
    if (!_allWelcomeImage) {
        _allWelcomeImage = @[@"555.jpg",@"666.jpg",@"777.jpg"];
    }
    return _allWelcomeImage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//tableHeaderView上的scrollerView
//    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];
//    buttonOne.frame = CGRectMake(0, 0, 395, 275);
//    [buttonOne setBackgroundImage:[UIImage imageNamed:@"555.jpg"] forState:UIControlStateNormal];
//    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
//    buttonTwo.frame = CGRectMake(395, 0, 395, 275);
//    [buttonTwo setBackgroundImage:[UIImage imageNamed:@"666.jpg"] forState:UIControlStateNormal];
//    UIButton *buttonTrd = [UIButton buttonWithType:UIButtonTypeCustom];
//    buttonTrd.frame = CGRectMake(790, 0, 395, 275);
//    [buttonTrd setBackgroundImage:[UIImage imageNamed:@"777.jpg"] forState:UIControlStateNormal];
//    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 395, 275)];
//    scrollView.contentSize = CGSizeMake(395 * 3, 275);
//    scrollView.pagingEnabled = YES;
//    scrollView.showsHorizontalScrollIndicator = NO;
//    scrollView.showsVerticalScrollIndicator = NO;
//    scrollView.userInteractionEnabled=YES;
//    scrollView.delegate = self;
//    [scrollView addSubview:buttonOne];
//    [scrollView addSubview:buttonTwo];
//    [scrollView addSubview:buttonTrd];
//    [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(ScrollTimer) userInfo:nil repeats:YES];
////pageController
//    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(175, 245, 30, 10)];
//    pageControl.numberOfPages = 3;
//    pageControl.tintColor = [UIColor blackColor];
//    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//    pageControl.currentPage = 0;
//    [pageControl addTarget:self action:@selector(pageNumberChanged:) forControlEvents:UIControlEventValueChanged];
//    self.mainPage = pageControl;
//    self.mainScrollView = scrollView;
//    self.tableView.tableHeaderView = scrollView;
//    [self.view addSubview:pageControl];
    
    
    CGFloat width = 395.0;
    CGFloat height = 275.0;
    NSMutableArray *arrViews = [[NSMutableArray alloc] initWithCapacity:3];
    NSArray *arrImages = [[NSArray alloc]initWithObjects:@"555.jpg",@"666.jpg",@"777.jpg", nil];
    for (NSInteger i = 0; i < 3; i++) {
//        UIView *welcomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        UIImageView *welcomeImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        welcomeImage.image = [UIImage imageNamed:arrImages[i]];
//        welcomeView.backgroundColor = [arrColor objectAtIndex:i];
        
//        [welcomeView addSubview:welcomeImage];
        [arrViews addObject:welcomeImage];
    }
    [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(ScrollTimer) userInfo:nil repeats:YES];
    _welcomeView = [[WelcomeView alloc] initPageViewWithFrame:CGRectMake(10, 10, width, height) views:arrViews selectType:FRPCustomScrollView_SelectType_CanSelected];
    _welcomeView.pageViewDelegate = self;
    self.tableView.tableHeaderView = _welcomeView;
    
    UINavigationBar *titleBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 400, 20)];
    titleBar.backgroundColor = [UIColor lightGrayColor];
    UIBarButtonItem *searchBar = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchPage)];
    self.navigationItem.rightBarButtonItem = searchBar;
    self.navigationItem.title = @"全民直播";
    
    _itemView = [[leftItemView alloc]init];
    _itemView.pageChangeDelegate = self;
    
    [self.tableView reloadData];
    
}
//#pragma mark --- scrollview自动滚动
//#pragma mark --- scrollviewDelegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    int page = self.mainScrollView.contentOffset.x / 395;//通过滚动的偏移量来判断目前页面所对应的小白点
//    self.mainPage.currentPage = page;
//}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//}
//
////- (void)pageNumberChangerd:(UIPageControl *)pageControl{
////    if (pageControl.currentPage) {
////        <#statements#>
////    }
////}
//
- (void)ScrollTimer{
    int timeCounter;
    timeCounter++;
    if(timeCounter == 4){
        timeCounter = 1;
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
    }
    [self.mainScrollView setContentOffset:CGPointMake(400*timeCounter, 0)];
}

#pragma mark - FRPCustomPageViewDelegate
- (void)pageViewDidClickedWithPageNumber:(NSInteger)pageNumber{
    NSLog(@"selected pageNumber is %ld",(long)pageNumber);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"点击了第%ld个view",(long)pageNumber]
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil, nil];
    
    [alert show];
}

#pragma mark - leftItemViewDelegate
-(void)pageDidClickWithPlayerName:(NSString *)playerName{
    NSLog(@"代理方法执行了");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"玩家是:%@",playerName]
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil, nil];
    
    [alert show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%f",tableView.bounds.size.width);
    static NSString *identifier = @"HomeCell";
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:self options:nil]lastObject];
    }
    
    FRPGame *game = [[FRPGame alloc]init];
    
    NSMutableArray *gameArray = [[NSMutableArray alloc]init];
    [gameArray addObjectsFromArray:[GameManager videos]];
//    game = gameArray[indexPath.section];
//    cell.leftView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
//    cell.leftView.leftLabel.text = game.gamePlayer;
//    cell.rightView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
//    cell.rightView.leftLabel.text = game.gamePlayer;
    if (indexPath.section == 0 && indexPath.row == 0) {
            game = gameArray[0];
            cell.leftView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
            cell.leftView.leftLabel.text = game.gamePlayer;
            game = gameArray[1];
            cell.rightView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
            cell.rightView.leftLabel.text = game.gamePlayer;
    }else if (indexPath.section == 0 && indexPath.row == 1){
        game = gameArray[3];
        cell.leftView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
        cell.leftView.leftLabel.text = game.gamePlayer;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        game = gameArray[2];
        cell.leftView.leftImageView.image = [UIImage imageNamed:game.gameIcon];
        cell.leftView.leftLabel.text = game.gamePlayer;
    }
    
    cell.game = game;

    cell.leftView.pageChangeDelegate = self;

//    FRPGame *game = [GameManager videos][indexPath.];
//    NSLog(@"game:%@",game);
//    
////    cell.leftView.leftImageView.backgroundColor = [UIColor redColor];
////    cell.rightView.leftImageView.backgroundColor = [UIColor blueColor];
//    cell.game = game;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    return cell;
}


//-(void)seguePage:(id)sender{
//    UIButton *bb = sender;
//    if (bb.tag == 1) {
//        GameLivingViewController *gameLVC = [[GameLivingViewController alloc]initWithNibName:@"GameLivingViewController" bundle:nil];
//        [self.navigationController pushViewController:gameLVC animated:YES];
//    }else if (bb.tag == 2){
//        GameLivingViewController *gameLVC = [[GameLivingViewController alloc]initWithNibName:@"GameLivingViewController" bundle:nil];
//        [self.navigationController pushViewController:gameLVC animated:YES];
//    }
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 275, 400, 20)];
    [button addTarget:self action:@selector(gamePageTurn) forControlEvents:UIControlEventTouchUpInside];
//    button.backgroundColor = [UIColor orangeColor];
    [self.tableView addSubview:button];
    UILabel *sectionHead = [[UILabel alloc]init];
    if (section == 0) {
        sectionHead.text = @"炉石传说";
    }else if (section == 1){
        sectionHead.text = @"愤怒的小鸟";
    }else{
        sectionHead.text = @"星际争霸";
    }
//    [button addSubview:sectionHead];
    return sectionHead;
}

//navigationBar 后面搜索按钮的方法
- (void)searchPage{
    SearchTableViewController *searchTVC = [[SearchTableViewController alloc]initWithNibName:@"SearchTableViewController" bundle:nil];
    [self.navigationController pushViewController:searchTVC animated:YES];
}
//点击图片跳转方法
//- (void)gameLivingPageTurn:(id)sender
//{
//    UIButton* btn = (UIButton*)sender;
//    NSLog(@"send is:%@",sender);
////- (void)gameLivingPageTurn:(id)sender{
//    if( btn.tag ==  1){
//        GameLivingViewController *gameLVC = [[GameLivingViewController alloc]initWithNibName:@"GameLivingViewController" bundle:nil];
//        [self.navigationController pushViewController:gameLVC animated:YES];
//    }
//    else if( btn.tag == 2){
//        GameLivingViewController *gameLVC = [[GameLivingViewController alloc]initWithNibName:@"GameLivingViewController" bundle:nil];
//        [self.navigationController pushViewController:gameLVC animated:YES];
//    }
//}
//点击section头跳转至播放列表
- (void)gamePageTurn{
    GameListTableViewController *gameLTVC = [[GameListTableViewController alloc]initWithNibName:@"GameListTableViewController" bundle:nil];
    [self.navigationController pushViewController:gameLTVC animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
